{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [],
   "source": [
    "%classpath add ../../../../../../target/classes\n",
    "import designpattern.adapter.tree.*;\n",
    "import java.util.stream.Collectors;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 示例简介\n",
    "显示一个组织结构的树形结构\n",
    "```plaintext\n",
    "  └── John\n",
    "    ├── Mary\n",
    "    │   ├── Joe\n",
    "    │   └── Jane\n",
    "    └── Jim\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 领域模型\n",
    "下面是一个简单的领域模型，用于表示一个组织结构。这个领域模型与树形结构的显示没有直接关系，在一个应用中，这是一个被很多其它模块使用的公共的领域模型。\n",
    "\n",
    "我们可以通过适配器模式将这个领域模型适配成树形结构。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [],
   "source": [
    "class Employee {\n",
    "    private String name;\n",
    "    private String superviser;\n",
    "\n",
    "    public Employee(String name, String superviser) {\n",
    "        this.name = name;\n",
    "        this.superviser = superviser;\n",
    "    }\n",
    "\n",
    "    public String getName() {\n",
    "        return name;\n",
    "    }\n",
    "\n",
    "    public String getSuperviser() {\n",
    "        return superviser;\n",
    "    }\n",
    "}\n",
    "\n",
    "public class OrgManager {\n",
    "    public List<Employee> getEmployees() {\n",
    "        return Arrays.asList(\n",
    "                new Employee(\"John\", null),\n",
    "                new Employee(\"Mary\", \"John\"),\n",
    "                new Employee(\"Jim\", \"John\"),\n",
    "                new Employee(\"Joe\", \"Mary\"),\n",
    "                new Employee(\"Jane\", \"Mary\"));\n",
    "    }\n",
    "    private static OrgManager orgManager = new OrgManager();\n",
    "    public static OrgManager instance() {\n",
    "        return orgManager;\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 适配器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [],
   "source": [
    "TreeContentProvider<Employee> contentProvider = new TreeContentProvider<Employee>() {\n",
    "\n",
    "    @Override\n",
    "    public List<Employee> getRoots() {\n",
    "        return OrgManager.instance().getEmployees()\n",
    "            .stream().filter(c -> c.getSuperviser() == null)\n",
    "            .collect(Collectors.toList());\n",
    "    }\n",
    "\n",
    "    @Override\n",
    "    public List<Employee> getChildren(Employee parent) {\n",
    "        return OrgManager.instance().getEmployees()\n",
    "            .stream().filter(c -> parent.getName().equals(c.getSuperviser()))\n",
    "            .collect(Collectors.toList());\n",
    "\n",
    "    }\n",
    "};\n",
    "\n",
    "NameProvider<Employee> nameProvider = new NameProvider<Employee>() {\n",
    "    @Override\n",
    "    public String getName(Employee employee) {\n",
    "        return employee.getName();\n",
    "    }\n",
    "};"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用简单的树形结构显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "John\n",
      "    Mary\n",
      "        Joe\n",
      "        Jane\n",
      "    Jim\n"
     ]
    }
   ],
   "source": [
    "new SimpleTreeViewer<Employee>(\n",
    "        contentProvider,\n",
    "        nameProvider\n",
    ").show();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用可视化的树形结构显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "vscode": {
     "languageId": "java"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "└── John\n",
      "    ├── Mary\n",
      "    │   ├── Joe\n",
      "    │   └── Jane\n",
      "    └── Jim\n"
     ]
    }
   ],
   "source": [
    "new VisualTreeViewer<Employee>(\n",
    "        contentProvider,\n",
    "        nameProvider\n",
    ").show();"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".jshell",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "11.0.21+9-LTS"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
